HTTP (HyperText Transfer Protocol) হল একটি প্রোটোকল যা ক্লায়েন্ট এবং সার্ভারের মধ্যে যোগাযোগের জন্য ব্যবহৃত হয়। এটি মূলত client-server architecture এর মাধ্যমে কাজ করে, যেখানে ক্লায়েন্ট সার্ভারকে request পাঠায় এবং সার্ভার তার অনুরোধ অনুযায়ী response পাঠায়। HTTP একটি stateless প্রোটোকল, যার মানে হল যে সার্ভার প্রতিটি অনুরোধের জন্য আলাদাভাবে প্রক্রিয়া করে এবং আগের অনুরোধের সাথে কোন সম্পর্ক রাখে না।
Apache HTTP Client হল একটি লাইব্রেরি যা Java অ্যাপ্লিকেশনগুলিকে HTTP প্রোটোকল ব্যবহার করে সার্ভারের সাথে যোগাযোগ করতে সাহায্য করে। এই লাইব্রেরি HTTP এর বিভিন্ন মেথড যেমন GET, POST, PUT, DELETE সমর্থন করে, যা ওয়েব সার্ভিসে ডেটা পাঠানো এবং গ্রহণ করার জন্য ব্যবহৃত হয়।
এখানে, আমরা GET, POST, PUT, এবং DELETE HTTP মেথডের ধারণা এবং Apache HTTP Client এর মাধ্যমে তাদের ব্যবহার দেখবো।
GET হল HTTP এর একটি মৌলিক মেথড যা সার্ভার থেকে ডেটা প্রাপ্তির জন্য ব্যবহৃত হয়। এটি সাধারণত একটি রিড অপারেশন হিসেবে কাজ করে, যার মাধ্যমে সার্ভার থেকে কোনো রিসোর্স (যেমন HTML পৃষ্ঠা, JSON ডেটা, ইমেজ) রিট্রিভ করা হয়। GET অনুরোধ সাধারণত কোনো শরীর (body) ধারণ করে না, শুধু URL এবং হেডার থাকে।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientGetExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP GET request
HttpGet request = new HttpGet("https://api.example.com/data");
// Send the request and get the response
HttpResponse response = httpClient.execute(request);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
POST মেথডটি HTTP প্রোটোকলে ডেটা পাঠানোর জন্য ব্যবহৃত হয়। এটি সাধারণত form submissions বা API requests এ ব্যবহৃত হয়, যেখানে ক্লায়েন্ট সার্ভারে কিছু ডেটা (যেমন JSON, XML, অথবা form data) পাঠায়। POST মেথডে সাধারণত request body থাকে, যেখানে ডেটা পাঠানো হয়।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientPostExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP POST request
HttpPost post = new HttpPost("https://api.example.com/submit");
// Set the body of the POST request
String json = "{\"name\":\"John\", \"age\":30}";
post.setEntity(new StringEntity(json));
post.setHeader("Content-Type", "application/json");
// Send the request and get the response
HttpResponse response = httpClient.execute(post);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
PUT মেথডটি সাধারণত সম্পদ আপডেট করার জন্য ব্যবহৃত হয়। এটি সম্পূর্ণ নতুন ডেটা দিয়ে একটি নির্দিষ্ট রিসোর্স প্রতিস্থাপন করে। PUT মেথডে ক্লায়েন্ট সার্ভারে নতুন ডেটা পাঠায় এবং সার্ভার এই ডেটা গ্রহণ করে সেই রিসোর্সটি আপডেট করে।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientPutExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP PUT request
HttpPut put = new HttpPut("https://api.example.com/update");
// Set the body of the PUT request
String json = "{\"name\":\"John\", \"age\":35}";
put.setEntity(new StringEntity(json));
put.setHeader("Content-Type", "application/json");
// Send the request and get the response
HttpResponse response = httpClient.execute(put);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
DELETE মেথডটি একটি নির্দিষ্ট রিসোর্স বা ডেটা মুছে ফেলতে ব্যবহৃত হয়। এটি একটি সম্পূর্ণ রিসোর্স বা ডেটা সার্ভার থেকে সরিয়ে দেয়। সাধারণত RESTful APIs এ DELETE মেথড ব্যবহৃত হয়।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientDeleteExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP DELETE request
HttpDelete delete = new HttpDelete("https://api.example.com/delete/123");
// Send the request and get the response
HttpResponse response = httpClient.execute(delete);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
123
দেওয়া হয়েছে, যা মুছে ফেলা হবে।GET, POST, PUT, এবং DELETE হল HTTP এর চারটি প্রধান মেথড, যা ওয়েব সার্ভিস এবং RESTful API এর মাধ্যমে যোগাযোগে ব্যবহৃত হয়। এই মেথডগুলি বিভিন্ন কার্যক্রমের জন্য ব্যবহৃত হয়:
Apache HTTP Client লাইব্রেরি ব্যবহার করে আপনি এই HTTP মেথডগুলি খুব সহজে অ্যাপ্লিকেশনে ইমপ্লিমেন্ট করতে পারেন এবং ওয়েব সার্ভিসের সাথে যোগাযোগ করতে পারেন।
common.read_more